

WIDTH=16
HEIGHT=16

CHARS = {
    "A": 177,
    "B": 178,
    "C": 179,
    "D": 180,
    "E": 181,
    "F": 182,
    "G": 183,
    "H": 184,
    "I": 185,
    "J": 186,
    "K": 187,
    "L": 188,
    "M": 189,
    "N": 190,
    "O": 191,
    "P": 160,
    "Q": 161,
    "R": 162,
    "S": 163,
    "T": 164,
    "U": 165,
    "V": 166,
    "W": 167,
    "X": 168,
    "Y": 169,
    "Z": 170,
    "a": 145,
    "b": 146,
    "c": 147,
    "d": 148,
    "e": 149,
    "f": 150,
    "g": 151,
    "h": 152,
    "i": 153,
    "j": 154,
    "k": 155,
    "l": 156,
    "m": 157,
    "n": 158,
    "o": 159,
    "p": 128,
    "q": 129,
    "r": 130,
    "s": 131,
    "t": 132,
    "u": 133,
    "v": 134,
    "w": 135,
    "x": 136,
    "y": 137,
    "z": 138,
    "0": 192,    
    "1": 193,
    "2": 194,
    "3": 195,
    "4": 196,
    "5": 197,
    "6": 198,
    "7": 199,
    "8": 200,
    "9": 201,
    " ": 240,
    "!": 209,
    "\"": 210,
    "#": 211,
    "$": 212,
    "%": 213,
    "&": 214,
    "'": 215,
    "(": 216,
    ")": 217,
    "*": 218,
    "+": 219,
    ",": 220,
    "-": 221,
    ".": 222,
    "/": 223,
    ":": 202,
    ";": 203,
    "<": 204,
    "=": 205,
    ">": 206,
    "?": 207,
    "[": 171,
    "\\": 172,
    "]": 173,
    "^": 174,
    "_": 175,
}

FRAME = {
    7: 57,
    8: 61,
    9: 75,
    4: 74,
    6: 74,
    1: 56,
    2: 61,
    3: 76,
    5: 240,
}


import pyglet
from pyglet.gl import *
import data


image = pyglet.image.load(data.filepath("extended_ascii2.png"))
tileGrid = pyglet.image.ImageGrid(image, 16, 16)

LINE_HEIGHT = 3

PASSABLE=0
SOLID=1


PLAYER = 176
NPC = 241
NPC2 = 242
PORTAL = 255

SWITCH_ON = 175
SWITCH_OFF = 223

LEVEL_TILES = {
    7: (57, SOLID),
    9: (75, SOLID),
    4: (74, SOLID),
    6: (74, SOLID),
    1: (56, SOLID),
    3: (76, SOLID),
    8: (61, SOLID),
    2: (61, SOLID),
    5: (65,PASSABLE),
    0: (PORTAL, PASSABLE),
}


def string2tiles(s):
    returnTiles = []
    for c in s:
        returnTiles.append(CHARS[c])
    return returnTiles


def buildframe(width,height):
    returnTiles = []
    for row in range(0,height):
        tmpRow = []
        for col in range(0, width):
            if row == 0:
                if col == 0:
                    tmpRow.append(FRAME[9])
                elif col == width-1:
                    tmpRow.append(FRAME[7])
                else:
                    tmpRow.append(FRAME[8])
            elif row == height-1:
                if col == 0:
                    tmpRow.append(FRAME[3])
                elif col == width-1:
                    tmpRow.append(FRAME[1])
                else:
                    tmpRow.append(FRAME[8])
            else:
                if col == 0:
                    tmpRow.append(FRAME[4])
                elif col == width-1:
                    tmpRow.append(FRAME[6])
                else:
                    tmpRow.append(FRAME[5])
        returnTiles.append(tmpRow)
    return returnTiles


def createFrameSprite(w, h):
    frameTex = pyglet.image.Texture.create(w*16,h*16, GL_RGBA)
    frameTiles = buildframe(w,h)
    r, c = 0,0
    for row in frameTiles:
        for col in row:
            x, y =  (w*16) - (c*16) - 16, (h*16) - (r*16) - 16
            #print r, c, col, "(", x, y, ")"
            tileGrid[col].blit_to_texture(frameTex.target, 
                0, x, y, GL_RGBA)
            c += 1
        c = 0
        r += 1
    sprite = pyglet.sprite.Sprite(frameTex)
    sprite.image.anchor_x = sprite.image.width/2
    sprite.image.anchor_y = sprite.image.height/2
    return sprite

def createLevelSprite(w, h, levelTiles):
    frameTex = pyglet.image.Texture.create(w*16,h*16, GL_RGBA)
    r, c = 0,0
    for row in levelTiles:
        for col in row:
            x, y =  (w*16) - (c*16) - 16, (h*16) - (r*16) - 16
            #print r, c, col, "(", x, y, ")"
            tileGrid[col].blit_to_texture(frameTex.target, 
                0, x, y, GL_RGBA)
            c += 1
        c = 0
        r += 1
    sprite = pyglet.sprite.Sprite(frameTex)
    sprite.image.anchor_x = sprite.image.width/2
    sprite.image.anchor_y = sprite.image.height/2
    return sprite

def createStringSprite(s):
    stringTiles = string2tiles(s)
    frameTex = pyglet.image.Texture.create(len(stringTiles)*16, 16, GL_RGBA)
    c = 0
    for col in stringTiles:
        x, y =  (c*16), 0
        print c, col, "(", x, y, ")"
        tileGrid[col].blit_to_texture(frameTex.target, 
            0, x, y, GL_RGBA)
        c+=1
    sprite = None
    return pyglet.sprite.Sprite(frameTex)


if __name__ == '__main__':
    print string2tiles("Hello World")
    for r in buildframe(10,10):
        print r



